﻿using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.TeamFoundation.Framework.Client;
namespace Tfs_analyzer
{
  class ValidationResultXMLWriter
    {
    public void WriteReport(string i_path, ValidationSummary i_summary)
      {
      XmlWriter writer = XmlWriter.Create(i_path + "\\report.xml");

     writer.WriteStartDocument();
     writer.WriteStartElement("Report");
     writer.WriteElementString("Project", i_summary.project);

     //foreach (KeyValuePair<DateTime, Dictionary<Changeset, List<IValidationResult>>> data in i_summary.m_validation_info)
     //  {
     //  writer.WriteStartElement("Day");
     //  writer.WriteElementString("Date", data.Key.ToString());
     //
     //  writer.WriteStartElement("Changesets");
     //  writer.WriteStartElement("ResultsOverview");
     //  writer.WriteElementString("ChangesetsCount", data.Value.Count.ToString());
     //  writer.WriteElementString("TotalScore", _CalculateTotalScore(data.Value).ToString());
     //  writer.WriteEndElement();
     //  foreach (KeyValuePair<Changeset, List<IValidationResult>> res in data.Value)
     //    {
     //    writer.WriteStartElement("Changeset");
     //    writer.WriteElementString("ID", res.Key.ChangesetId.ToString());
     //    writer.WriteElementString("Committer", res.Key.Owner.ToString());
     //    writer.WriteElementString("CreationDate", res.Key.CreationDate.ToString());
     //    //  writer.WriteElementString("CheckinNote", res.Key.CheckinNote.ToString());
     //    writer.WriteElementString("Comment", res.Key.Comment);
     //
     //    writer.WriteStartElement("WorkItems");
     //    foreach (AssociatedWorkItemInfo wi in res.Key.AssociatedWorkItems)
     //      {
     //      writer.WriteStartElement("WI");
     //      writer.WriteElementString("Title", wi.Title.ToString());
     //      writer.WriteElementString("ID", wi.Id.ToString());
     //      writer.WriteElementString("Type", wi.WorkItemType.ToString());
     //      writer.WriteEndElement();
     //      }
     //    writer.WriteEndElement();
     //
     //    writer.WriteStartElement("Validation");
     //    writer.WriteElementString("ChangesetScore", _CalculateValidationTotal(res.Value).ToString());
     //    foreach (IValidationResult val_res in res.Value)
     //      {
     //      writer.WriteStartElement("ValidationInfo");
     //      writer.WriteElementString("mark", val_res.GetMark().ToString());
     //      writer.WriteElementString("reason", val_res.GetReason().ToString());
     //      writer.WriteEndElement();
     //      }
     //    writer.WriteEndElement();
     //    writer.WriteEndElement();
     //    }
     //
     //  writer.WriteEndElement();
     //  writer.WriteEndElement();
     //  }
      writer.WriteEndElement();


    
      writer.WriteEndDocument();
      writer.Flush();
      writer.Close();  
      }

    private int _CalculateTotalScore(Dictionary<Changeset, List<IValidationResult>> i_report_data)
      {
      int score = 0;
      foreach (KeyValuePair<Changeset, List<IValidationResult>> res in i_report_data)
        {
        score += _CalculateValidationTotal(res.Value);
        }
      return score;
      }

    private int _CalculateValidationTotal(List<IValidationResult> i_val_results)
      {
      int score = 0;
      foreach (IValidationResult val_res in i_val_results)
        {
        score += val_res.GetMark(); 
        }
      return score;
      }
    }
}
